package com.ghbank.rdws.management.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ghbank.rdws.biz.service.SysDataSourceConfigService;
import com.ghbank.rdws.common.entity.SysDataSourceConfig;
import com.google.common.collect.Sets;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class SysDataSourceConfigController {
    
    @Autowired
    private SysDataSourceConfigService sysDataSourceConfigService;
    
    @GetMapping(value = "/sys/datasource")
    public List<SysDataSourceConfig> findConfigs(@RequestParam(value = "datasourceIds", required = false) String appIds) {
      if (StringUtils.isEmpty(appIds)) {
        return sysDataSourceConfigService.findAll();
      } else {
        return sysDataSourceConfigService.findByIds(Sets.newHashSet(appIds.split(",")));
      }

    }
    
    @GetMapping(value = "/sys/datasource/{id}")
    public SysDataSourceConfig querySysDataSource(@PathVariable long id) {
        try {
            SysDataSourceConfig config = sysDataSourceConfigService.findById(id);
            return config;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            throw new RuntimeException(e.getMessage(),e);
        }
    }
}
